﻿using EYangFrame.DataAccess.Global;
using EYangFramework.Business.SystemManager.Interfaces;
using EYangFramework.Common.Ninject;
using EYangFramework.Domain.Entitys.ValueObjects;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EYangFramework.SystemManager
{
    //注意,因为EF机制的问题,在调用BLL层的时候,必须使用using语句来操作,否则会报错,这个是仓储层实现的问题,可以对其进行改进
    //所有的BLL层必须继承自IBLLBase接口,以便规范业务层的实际实现,在Dispose方法中,需要对DbContext进行释放,否则会造成一个
    //实体被分多个上下文引用的错误
    public partial class frm_NavigationManager : EYangFramework.CommonForm.frm_BasicForm
    {
        List<Navigation> navigations;
        Navigation selectedNavigation;
        public frm_NavigationManager()
        {
            this.AllowSingtonTimer = false;
            InitializeComponent();
        }
        public override MenuStrip GetMenuMain()
        {
            return this.menuStrip1;
        }

        private void frm_NavigationManager_Load(object sender, EventArgs e)
        {
            this.Text = "导航管理";
            SetTabVisible(tp_Detail, false);
            InitTreeView();
        }
        private void InitTreeView()
        {
            tv_List.Nodes.Clear();
            using (IBLLNavigation BLLNavigation = NinjectProvider.NinjectCore.GetService<IBLLNavigation>())
            {
                navigations = BLLNavigation.GetAllList();
                TreeNode treeNode = new TreeNode();
                treeNode.Text = "导航清单";
                treeNode.Tag = null;
                tv_List.Nodes.Add(treeNode);
                ShowNavigationList(treeNode, navigations.Where(t => t.PREVIEWNAVIGATION == null).OrderBy(t=>t.ORDER).ToList());
                tv_List.ExpandAll();
            }
        }

        private void ShowNavigationList(TreeNode parent,List<Navigation> currentNavigations)
        {
            foreach (var item in currentNavigations)
            {
                TreeNode node = new TreeNode();
                node.Text = item.NAVIGATIONNAME;
                node.Tag = item.ID.ToString();
                parent.Nodes.Add(node);

                List<Navigation> childNavigations = navigations.Where(t => t.PREVIEWNAVIGATION?.ID == item.ID).OrderBy(t=>t.ORDER).ToList();
                if (childNavigations.Count > 0)
                {
                    ShowNavigationList(node, childNavigations);
                }
            }
        }

        private void BindNavigtionToDetail(Navigation navigation)
        {
            txtNavigationName.Text = navigation?.NAVIGATIONNAME;
            txtNavigationUrl.Text = navigation?.URL;
            txtNavigationOrder.Text = navigation?.ORDER.ToString();
            txtNavigationBGImage.Text = navigation?.BGIMAGE;
            comNavigationType.SelectedItem = navigation?.NAVIGATIONTYPE.ToString();
        }
        private void menu_Add_Click(object sender, EventArgs e)
        {
            var node = tv_List.SelectedNode;
            if (node == null)
            {
                MessageBox.Show("请选择需要新增子节点的节点!");
                return;
            }
            if (node.Tag != null)
            {
                var currentNavigation = navigations.Where(t => t.ID.ToString() == node.Tag.ToString()).FirstOrDefault();
                selectedNavigation = currentNavigation;
                if (currentNavigation.NAVIGATIONTYPE == NavigationType.Form)
                {
                    MessageBox.Show("窗体不能添加子节点!");
                    return;
                }
            }
            else
            {
                selectedNavigation = null;
            }
            groupEdit.Text = $"您正在为{node.Text}新增节点!";
            groupEdit.Tag = null;
            btnSave.Visible = true;
            tableLayoutPanel1.Enabled = true;
            BindNavigtionToDetail(null);
            SetTabVisible(tp_Detail, true);
        }
        private void menu_View_Click(object sender, EventArgs e)
        {
            var node = tv_List.SelectedNode;
            if (node == null)
            {
                MessageBox.Show("请选择需要查看的节点!");
                return;
            }
            if (node.Tag == null)
            {
                MessageBox.Show("根节点不能选择,请重新选择!");
                return;
            }
            var currentNavigation = navigations.Where(t => t.ID.ToString() == node.Tag.ToString()).FirstOrDefault();
            groupEdit.Text = $"你正在查看{node.Text}节点!";
            btnSave.Visible = false;
            BindNavigtionToDetail(currentNavigation);
            tableLayoutPanel1.Enabled = false;
            SetTabVisible(tp_Detail, true);
        }

        private void menu_Modify_Click(object sender, EventArgs e)
        {
            var node = tv_List.SelectedNode;
            if (node == null)
            {
                MessageBox.Show("请选择需要编辑的节点!");
                return;
            }
            if (node.Tag == null)
            {
                MessageBox.Show("根节点不能编辑,请重新选择!");
                return;
            }
            var currentNavigation = navigations.Where(t => t.ID.ToString() == node.Tag.ToString()).FirstOrDefault();
            groupEdit.Text = $"你正在编辑{node.Text}节点!";
            groupEdit.Tag = currentNavigation.ID.ToString();
            btnSave.Visible = true;
            BindNavigtionToDetail(currentNavigation);
            tableLayoutPanel1.Enabled = true;
            SetTabVisible(tp_Detail, true);
        }

        private void menu_Delete_Click(object sender, EventArgs e)
        {
            var node = tv_List.SelectedNode;
            if (node == null)
            {
                MessageBox.Show("请选择需要删除的节点!");
                return;
            }
            if (node.Tag == null)
            {
                MessageBox.Show("根节点不能删除,请重新选择!");
                return;
            }
            if (MessageBox.Show("你确定需要删除选中的节点吗?删除后所有用户将不再拥有其操作权限,且无法恢复,请务必谨慎操作!", "警告!", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                using (IBLLNavigation BLLNavigation = NinjectProvider.NinjectCore.GetService<IBLLNavigation>())
                    BLLNavigation.DeletSelectedNavigation(Guid.Parse(node.Tag.ToString()));
                InitTreeView();
            }
        }

        private void tv_List_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            menu_View_Click(null, null);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你确定要退出当前编辑状态吗?", "提醒!", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                SetTabVisible(tp_Detail, false);
            }
        }

        private void txtNavigationBGImage_DoubleClick(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtNavigationBGImage.Text = openFileDialog1.FileName;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtNavigationName.Text))
            {
                MessageBox.Show("导航名称不能为空!");
                return;
            }
            if (comNavigationType.SelectedItem == null)
            {
                MessageBox.Show("请指定当前导航的类型!");
                return;
            }
            if (comNavigationType.SelectedItem.ToString() == "Form"&& string.IsNullOrEmpty(txtNavigationUrl.Text))
            {
                MessageBox.Show("导航类型为Form时,必须指定窗体对应的类型!");
                return;
            }
            if (comNavigationType.SelectedItem.ToString() == "Module" && !string.IsNullOrEmpty(txtNavigationUrl.Text))
            {
                MessageBox.Show("导航类型为Module时,不能指定窗体类型!");
                return;
            }
            if (string.IsNullOrEmpty(txtNavigationOrder.Text))
            {
                MessageBox.Show("请指定当前导航的排列顺序!");
                return;
            }

            using (IBLLNavigation BLLNavigation = NinjectProvider.NinjectCore.GetService<IBLLNavigation>())
            {
                if (BLLNavigation.GetAllList().Any(t => t.NAVIGATIONNAME == txtNavigationName.Text))
                {
                    MessageBox.Show("当前导航名称已存在,请重新输入!");
                    return;
                }
                Navigation navigation;
                if (groupEdit.Tag == null)
                {
                    navigation = new Navigation();
                    navigation.PREVIEWNAVIGATION = selectedNavigation == null ? null : BLLNavigation.GetBusinessById(selectedNavigation.ID);
                }
                else
                {
                    navigation = BLLNavigation.GetBusinessById(Guid.Parse(groupEdit.Tag.ToString()));
                }
                navigation.NAVIGATIONNAME = txtNavigationName.Text;
                navigation.NAVIGATIONTYPE = (NavigationType)Enum.Parse(typeof(NavigationType), comNavigationType.SelectedItem.ToString());
                navigation.ORDER = Convert.ToInt32(txtNavigationOrder.Text);
                navigation.URL = txtNavigationUrl.Text;
                navigation.BGIMAGE = txtNavigationBGImage.Text;
                navigation.CREATEUSER = GlobalInfo.CurrentUser.USERNAME;
                var result = groupEdit.Tag == null ? BLLNavigation.InsertNavigation(navigation) : BLLNavigation.UpdateSelectedNavigation(navigation);
                if (result)
                {
                    MessageBox.Show("保存成功!");
                    SetTabVisible(tp_Detail, false);
                    InitTreeView();
                    return;
                }
            }
        }
    }
}
